<?php
require_once 'Translator/Model.php';

/**
 * User model.
 */
class UserModel extends Translator_Model {
	/**
	 * Fetch a user dataset using the forum itendifier.
	 * 
	 * @param string $identifier
	 * @return array
	 */
	public function fetch($identifier) {
		return $this->_db->fetchRow('SELECT * FROM users WHERE identifier = ? LIMIT 1',
			array($identifier));
	}
	
	/**
	 * Create user
	 * 
	 * @param $identifier
	 * @param $email
	 */
	public function create($identifier, $email) {
		$this->_db->insert('users', array(
			'identifier' => $identifier,
			'email' => $email
		));
	}
	
	/**
	 * Update the profile of the user
	 * 
	 * @param int $id of the user to change
	 * @param string $name
	 * @param string $email
	 */
	public function updateProfile($id, $name, $email) {
		$this->_db->update('users', array(
			'name' => $name,
			'email' => $email
		), 'id = '.intval($id));
	}
	
	/**
	 * Check if the given user has the permission to edit the given
	 * language. Notice that this method just checks if there is a
	 * corresponding entry in the database, not if the user is administrator
	 * (in which case there is none but he has permissions anyway).
	 * 
	 * Please use the user class method called canTranslate() to check
	 * if the current user can translate the passed language, this
	 * is more a helper method. 
	 * 
	 * @param int $userid
	 * @param int $languageid
	 * @see Translator_User#canTranslate()
	 */
	public function canTranslate($userid, $languageid) {
		$count = $this->_db->fetchSingle(
			'SELECT COUNT(*) AS count
			 FROM user_languages 
			 WHERE user = ? AND language = ?',
			array($userid, $languageid)
		);
		
		return $count > 0;
	}
}